package tools.avonigo.com;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Properties;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class Util {

	private static final Logger log = Logger.getLogger(Util.class.getName());
	
    public static Document stringToDom(String xmlSource) 
            throws SAXException, ParserConfigurationException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
         return doc;
    }
    
    public static String getProperties(String fileName, String key){
  	  
      	Properties prop = new Properties();
      	InputStream input = null;
      	
      	String folderRessources = Constantes.FOLDER_RESSOURCES + Constantes.SLASH;
      	String extention = Constantes.POINT + Constantes.EXTENTION_PROPERTIES;

      	try {

      		String fileProperties = folderRessources+fileName+ extention;
      		input = Util.class.getClassLoader().getResourceAsStream(fileProperties);
      		if(input==null){
      	            log.info("Sorry, unable to find " + fileProperties);
      		    return null;
      		}

      		//load a properties file from class path, inside static method
      		prop.load(input);
      	} catch (IOException ex) {
      		ex.printStackTrace();
          } finally{
          	if(input!=null){
          		try {
    				input.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
          	}
          }
    	return prop.getProperty(key);
      
      }
    
}